<?php

class S
{
	static $load		=array();
	static $ojb			=array();
	static $var			=array();
	static $debug		=false;
	static $aMark		='a';
	static $mMark		='m';
	static $action		='';

	static function run()
	{
		$config = include ROOT.'data/config/system.config.php';
		foreach($config as $k => $v)
		{
			self::set($k, $v);
		}
		S::set('__Rs',implode(',', $_REQUEST));
		$classes=array('app','action','model','template','mysql','cache');
		S::loadClass($classes);
	}

	static function instance($ojb)
	{
		self::$ojb[]=$ojb;
	}

	static function uninstall($ojb)
	{
		if(isset(self::$ojb[$ojb]))
		{
			unset(self::$ojb[$ojb]);
		}
	}

	static function isOjb($ojb)
	{
		return in_array($ojb,self::$ojb);
	}

	static function set($name,$value)
	{
		self::$var[$name]=$value;
		return true;
	}

	static function add($name,$value)
	{
		self::$var[$name][]=$arg_array;
		return true;
	}

	static function get($name)
	{
		return self::$var[$name];
	}

	static function remove($name)
	{
		unset(self::$var[$name]);
	}

	static function isvar($name)
	{
		return isset(self::$var[$name]);
	}

	static function runTime()
	{
		return microtime(true)-$GLOBALS['st_time'];
	}



	static function autoAction()
	{
		$filename=ROOT.'source/action/'.APP.'.action.php';
		if(!is_file($filename))
		{
			S::loadClass('action');
			$appAction='action';
			//TO BE DONE
			self::error($msg);
		}else {
			require $filename;
			$appAction=APP.'Action';
		}
		return new $appAction();
	}

	static function autoModel()
	{
		return S::loadModel(APP);
	}

	static function loadModel($filename)
	{
		$filename=ROOT.'source/model/'.$filename.'.model.php';
		if(!is_file($filename))
		{
			S::loadClass('model');
			$appModel='model';
			//TO BE DONE
			self::error($msg);
		}else {
			require $filename;
			$appModel=APP.'Model';
		}
		return new $appModel();
	}

	static function loadClass($filename)
	{
		if(is_array($filename))
		{
			foreach($filename as $f)
			{
				$filename=ROOT.'source/core/'.$f.'.class.php';
				self::loadFile($filename);
			}
		}
		else 
		{
			$filename=ROOT.'source/core/'.$filename.'.class.php';
			self::loadFile($filename);
		}
	}

	static function loadFile($filename)
	{
		if(is_file($filename))
		{
			if(!in_array($filename,self::$load)){
				self::$load[]=$filename;
				require $filename;
			}
		}
		else
		{
			//to bo done
			self::error($msg);
		}
	}

	static function error($msg,$level=1)
	{

	}






	static function test($str)
	{
		file_put_contents(ROOT.'temp/test.txt', $str);
		return $str;
	}

}